import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class yichuyuanus {


//    public static void main(String[] args) {
//        //正则表达式的问题
//
//        StringBuffer stringBuffer = new StringBuffer("ni_shi-liqiang-ba");
//        System.out.println(stringBuffer.toString());
//
//        StringBuffer guojianquan = stringBuffer.replace(7, 13, "guojianquan");
//        System.out.println(guojianquan.toString());
//
//    }




    public static void main(String[] args) {
        //每次来的数据
        List<data> comes = new ArrayList<>();
        //需要存的内存缓存数据
        List<data>  youde=new ArrayList<>();

        youde.add(new data("1","bbbbb"));
        youde.add(new data("2","aaaaa"));
        youde.add(new data("3","789"));
        youde.add(new data("4","789"));

        comes.add(new data("1","cccc"));
        comes.add(new data("1","dddd"));
        comes.add(new data("3","78910"));

        for (data c : comes) {
            System.out.println(c);
        }

        if (comes!=null&&comes.size()>0){
            if (youde==null){
                youde=new ArrayList<>(comes);
            }else {
                //update
                for (data come : comes) {
                    boolean flag=false;
                    for (data data : youde) {
                        if (data.getCode().equals(come.getCode())){
                            Collections.replaceAll(youde,data,come);
                            flag=true;
                            break;
                        }
                    }
                    //add
                    if (!flag){
                        youde.add(come);
                    }
                }
                //delete
//                Iterator<data> iterator = youde.iterator();
//                while (iterator.hasNext()){
//                    data next = iterator.next();
//                    if (!comes.contains(next)){
//                        iterator.remove();
//                    }
//                }
            }
        }
        for (data data : youde) {
            System.out.println(data);
        }
    }
}
